package srcInterviewPrep;

import java.util.HashMap;

public final class MaxMindGame
{
    public static void PrintNumHitsAndPsedoHits(String actualColos, String guessedColors)
    {
        // Argument validations, not null, equal length, etc
        
        int hits = 0;
        int pseudoHits = 0;
        
        char[] actualChars = actualColos.toCharArray();
        char[] guessedChars = guessedColors.toCharArray();
        
        hits = CountHits(actualChars, guessedChars);
        pseudoHits = CountPseudoHits(actualChars, guessedChars);
        
        System.out.println("hits: " + hits);
        System.out.println("pseudo hits: " + pseudoHits);
    }
    
    private static int CountHits(char[] actualLetters, char[] guessedLetters)
    {
        int count = 0;
        
        for (int i = 0; i < actualLetters.length; ++i)
        {
            if (actualLetters[i] == guessedLetters[i])
            {
                ++count;
                actualLetters[i] = 'X';
                guessedLetters[i] = 'X';
            }
        }
        
        return count;
    }
    
    private static int CountPseudoHits(char[] actualLetters, char[] guessedLetters)
    {
        int count = 0;
        
        HashMap<Character, Integer> map = new HashMap();
        
        for (int i = 0; i < guessedLetters.length; ++i)
        {
            char letter = guessedLetters[i];
            if (letter == 'X')
            {
                continue;
            }
            
            map.put(
                letter, 
                !map.containsKey(letter) ?  1 : map.get(letter) + 1);
        }
        
        for (int i = 0; i < actualLetters.length; ++i)
        {
            char letter = actualLetters[i];
            if (letter == 'X')
            {
                continue;
            }
            
            if (map.containsKey(letter))
            {
                ++count;
                
                int newFrequency = map.get(letter) - 1;
                
                if (newFrequency == 0)
                {
                    map.remove(letter);
                }
                else
                {
                    map.put(letter, newFrequency);
                }
            }
        }
        
        return count;
    }
}